package vlms.service;

import javax.jws.WebService;
import javax.xml.rpc.soap.SOAPFaultException;
import javax.xml.soap.SOAPException;

import java.sql.*;
import java.util.Calendar;

import vlms.bean.*;
import vlms.dao.AccountDAO;
import vlms.dao.CartDAO;
import vlms.dao.IssuedmovieDAO;
import vlms.dao.MovieDAO;
import vlms.dao.PersonDAO;
import vlms.dao.PremiumcustomerDAO;
import vlms.dao.SimplecustomerDAO;
import vlms.exception.*;
import wrapper.CartMovie;
import wrapper.MovieIssue;

@WebService
public class VLMSService {

	//static AccountDAO accountDAO = new AccountDAO();
	//static CartDAO cartDAO = new CartDAO();
	//static IssuedmovieDAO issuedmovieDAO = new IssuedmovieDAO();
	//static MovieDAO movieDAO = new MovieDAO();
	//static PersonDAO personDAO = new PersonDAO();
	//static PremiumcustomerDAO premiumcustomerDAO = new PremiumcustomerDAO();
	//static SimplecustomerDAO simplecustomerDAO = new SimplecustomerDAO();

	//final String sqlMsg = "SQL Exception occured in the system . Please try " + "again later.";
	// Need to do connection pooling in Connection utility class TODO
	//Connection con = DB.getConn();

	/*
	protected static String personDetails(String result,Person person)
	{
		
		result += String.valueOf(person.getPersonid()) +":" + person.getEmailid() +":" + person.getFirstname() +":" + person.getLastname() 
				+":" + person.getCity() +":" + person.getState()+":" + person.getMembertype() ;
		
		return result;
		
	}
	
	protected static String simpleCustomerDetails(String result, SimpleCustomer simpleCustomer)
	{
		result += ":" +simpleCustomer.getMembershipno() + ":" + String.valueOf(simpleCustomer.getOutstandingmovie());
		return result;
	}
	
	protected static String premiumCustomerDetails(String result, PremiumCustomer premiumCustomer)
	{
		result +=  ":" + premiumCustomer.getMembershipno() +":" + String.valueOf(premiumCustomer.getOutstandingmovies());
		return result;
	}
	*/
	protected static String[] mergeIssuedMovieDetails(IssuedMovie[] issuedMovieList, Movie[] movieList)
	{
		String[] result=new String[issuedMovieList.length];
		
		for(int index=0;index<issuedMovieList.length;index++)
		{
			IssuedMovie issuedMovie=issuedMovieList[index];
			Movie movie=movieList[index];
			result[index]=String.valueOf(issuedMovie.getIssueid()) + ":" + String.valueOf(issuedMovie.getPersonid()) + ":" + movie.getMovieid() + ":" + movie.getMoviename() + ":" + movie.getCategory() + ":" 
		+ String.valueOf(movie.getRentamount()) + ":" + String.valueOf(issuedMovie.getIssuedate()) + ":" + String.valueOf(issuedMovie.getDuedate()) + ":" 
		+ String.valueOf(issuedMovie.getReturndate());		
		
			
		}
		
		return result;
	}
	
// Amit Sant //
	/**
	 * Delete Premium Customer by personId
	 * 
	 * @param personId Person identifier.
	 * @return Success or error string. 
	 * @throws SOAPException 
	 */
	public String deletePremiumCustomer(String personId) {
		String result= "";
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");		
			int iPersonId = Integer.parseInt(personId);
			
		    // Delete person from Person table.. As the delete cascade is on the person's information will
			// be deleted from account, cart, issued movies, PC tables.
			PersonDAO personDAO = new PersonDAO();
			personDAO.delete(iPersonId, con);
			con.commit();
			result = "Premium Customer successfully deleted from the system";
			return result;
		}
		catch(PersonException pe) {
		
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
		
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	/**
	 * Delete Simple Customer by personId
	 * 
	 * @param personId Person identifier.
	 * @return Success or error string.
	 */
	public String deleteSimpleCustomer(String personId) {
		String result = "";
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");		
			int iPersonId = Integer.parseInt(personId);
			
			// Delete person from Person table.. As the delete cascade is on the person's information will
			// be deleted from account, cart, issued movies, PC tables.
			PersonDAO personDAO = new PersonDAO();
			personDAO.delete(iPersonId, con);
			con.commit();
			result = "Premium Customer successfully deleted from the system";
			return result;
		}
		catch(PersonException pe) {
		
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
		
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Create a new movie in the system. 
	 * @param movie Reference to movie bean.
	 * @return Success or error string.
	 */
	public String createMovie(Movie movie) {
		String result = "";
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			MovieDAO movieDAO=new MovieDAO();
			movieDAO.insert(movie, con);
			con.commit();
			result="Movie created successfully";
			return result;
		}
		catch(MovieException pe) {
		
			
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		}

	/**
	 * Delete a movie from the system. 
	 * @param movieId Movie identifier.
	 * @return Success or error string.
	 */
	public String deleteMovie(String movieId) {
		String result = "";
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			//To set cascade on delete operation in db
			MovieDAO movieDAO=new MovieDAO();
			movieDAO.delete(Integer.parseInt(movieId), con);
			con.commit();
			result="Movie deleted successfully";
			return result;
		}
		catch(MovieException pe) {
		
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
		
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Called when person is searched by membership number
	 * 
	 * @param membershipNo Membership number.
	 * @return Person information + Customer Details 
	 */ 
	public String searchPersonByMemNumber(String membershipNo) {
		// in order to view the issued movie list, we will have a separate web service call 
		String result = "";
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			if(membershipNo.startsWith("1"))
			{
			SimplecustomerDAO simplecustomerDAO=new SimplecustomerDAO();
			SimpleCustomer simplecustomer=simplecustomerDAO.findByPrimaryKey(membershipNo, con);
			
			int personid=simplecustomer.getPersonid();
			
			PersonDAO personDAO = new PersonDAO();
			
			Person person=personDAO.findByPrimaryKey(personid, con);
			
			result=String.valueOf(person.getPersonid()) +":" + person.getEmailid() +":" + person.getFirstname() +":" + person.getLastname() 
			+":" + person.getCity() +":" + person.getState()+":" + person.getMembertype()+ ":" 
			+simplecustomer.getMembershipno() + ":" + String.valueOf(simplecustomer.getOutstandingmovie());
			}
			else
			{
				PremiumcustomerDAO premiumCustomerDAO=new PremiumcustomerDAO();
				PremiumCustomer premiumcustomer=premiumCustomerDAO.findByPrimaryKey(membershipNo, con);	
				
				int personid=premiumcustomer.getPersonid();
				
				PersonDAO personDAO = new PersonDAO();
				
				Person person=personDAO.findByPrimaryKey(personid, con);
				result=String.valueOf(person.getPersonid()) +":" + person.getEmailid() +":" + person.getFirstname() +":" + person.getLastname() 
				+":" + person.getCity() +":" + person.getState()+":" + person.getMembertype()+ ":" +premiumcustomer.getMembershipno() + ":" + String.valueOf(premiumcustomer.getOutstandingmovies());
			}
			con.commit();
			return result;
		}
		catch (SimplecustomerException sce) {
			throw new SOAPFaultException(null, sce.getMessage(), "Client", null);
		}
		catch (PremiumcustomerException pce) {
			throw new SOAPFaultException(null, pce.getMessage(), "Client", null);
		}
		catch (PersonException pe) {
			// TODO Auto-generated catch block
			throw new SOAPFaultException(null, pe.getMessage(), "Client", null);
		}
		catch(SQLException se) {
		
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

	/**
	 * Used when searching Person and his issued movies based on membership
	 * number
	 * @param membershipNo membership number
	 * @return Issued Movies + Movie Information 
	 */
	public String[] retrieveIssuedMoviesByMemNumber(String membershipNo) {
		int personid=0;
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			if(membershipNo.startsWith("1"))
			{
			SimplecustomerDAO simplecustomerDAO=new SimplecustomerDAO();
			SimpleCustomer simplecustomer=simplecustomerDAO.findByPrimaryKey(membershipNo, con);
			
			personid=simplecustomer.getPersonid();
			}
			else
			{
				PremiumcustomerDAO premiumCustomerDAO=new PremiumcustomerDAO();
				PremiumCustomer premiumcustomer=premiumCustomerDAO.findByPrimaryKey(membershipNo, con);	
				
				personid=premiumcustomer.getPersonid();
			}
			
			IssuedmovieDAO issuedMovieDAO=new IssuedmovieDAO();
			IssuedMovie[] issuedMovieList=issuedMovieDAO.findByPersonid(personid, con);
			
			Movie[] movieList=new Movie[issuedMovieList.length];
			MovieDAO movieDAO=new MovieDAO();
			int index=0;
			for(IssuedMovie issuedmovieObj : issuedMovieList)
			{
				movieList[index]=movieDAO.findByMovieId(issuedmovieObj.getMovieid(), con);
				index++;
			}
			
			String[] result=new String[issuedMovieList.length];
			
			for(int index1=0;index1<issuedMovieList.length;index1++)
			{
				IssuedMovie issuedMovie=issuedMovieList[index1];
				Movie movie=movieList[index1];
				result[index1]=String.valueOf(issuedMovie.getIssueid()) + ":" + String.valueOf(issuedMovie.getPersonid()) + ":" + movie.getMovieid() + ":" + movie.getMoviename() + ":" + movie.getCategory() + ":" 
			+ String.valueOf(movie.getRentamount()) + ":" + String.valueOf(issuedMovie.getIssuedstatus()) + ":" + String.valueOf(issuedMovie.getDuedate()) ;	
			
				System.out.println(result[index1]);
			}
			
			
			for(String temp:result)
			{
				System.out.println("here" +temp);
			}
			return result;			
		}
		catch (SimplecustomerException sce) {
			throw new SOAPFaultException(null, sce.getMessage(), "Client", null);
		}
		catch(MovieException pe) {
			
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch (PremiumcustomerException pce) {
			throw new SOAPFaultException(null, pce.getMessage(), "Client", null);
		}
		catch(IssuedmovieException ime) {
			
			throw new SOAPFaultException(null, ime.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	

	}

	/**
	 * Called on returning of an issued movie
	 * 
	 * @param issueId issued Movie id
	 * @return Success or Error String
	 * @note Transaction required for the web service
	 */
	public String changeIssuedMovieStatus(IssuedMovie issuedmovie) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			IssuedmovieDAO issuedMovieDAO=new IssuedmovieDAO();
			
			issuedMovieDAO.updateIssuedInfo(issuedmovie, con);
			
			String result="Issue Status of the movie successfully updated";
			con.commit();
			return result;
		}
		catch(IssuedmovieException ime) {
			
			throw new SOAPFaultException(null, ime.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	/**
	 * List Premium Customer
	 * 
	 * @return list of premium customers
	 */
	public PremiumCustomer[] listPremiumCustomer() {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			PremiumcustomerDAO premiumcustomerDAO=new PremiumcustomerDAO();
			PremiumCustomer[] premiumCustomerList=premiumcustomerDAO.findAll(con);
			con.commit();
			return premiumCustomerList;
		}
		catch(PremiumcustomerException ime) {
			
			throw new SOAPFaultException(null, ime.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * List Simple Customer
	 * 
	 * @return List of Simple Customer
	 */
	public SimpleCustomer[] listSimpleCustomer() {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			SimplecustomerDAO simplecustomerDAO=new SimplecustomerDAO();
			SimpleCustomer[] simpleCustomerList=simplecustomerDAO.findAll(con);
			con.commit();
			return simpleCustomerList;
		}
		catch(SimplecustomerException sce) {
			
			throw new SOAPFaultException(null, sce.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * List All Movies
	 * 
	 * @return movie list
	 */
	public Movie[] listAllMovies() {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			MovieDAO movieDAO=new MovieDAO();
			
			Movie[] movieList=movieDAO.findAll(con);
			
			return movieList;
		}
		catch(MovieException ime) {
			
			throw new SOAPFaultException(null, ime.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * List All Person
	 * @return person list
	 */
	public Person[] listAllPersons() {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			PersonDAO personDAO=new PersonDAO();
			
			Person[] personList=personDAO.findAll(con);
			con.commit();
			return personList;
		}
		catch(PersonException pe) {
			
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	

	/**
	 * Update Movie
	 * 
	 * @param movie
	 * @return Success or Error String
	 */
	public String updateMovie(Movie movie) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			MovieDAO movieDAO=new MovieDAO();
			
			movieDAO.update(movie.getMovieid(), movie, con);
			
			String result="Movie is successfully updated";
			con.commit();
			return result;
		}
		catch(MovieException ime) {
			
			throw new SOAPFaultException(null, ime.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	// Amit Sant //
	
	// HARSHIL SHAH //
	/**
	 * Search person based on person attributes (firstname, lastname, etc)
	 * Set the attribute of person by which you have search before calling this function
	 * @param person
	 * @return person information + customer details
	 */
	public String[] searchPerson(Person person) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			PersonDAO personDAO=new PersonDAO();
			Person[] personList=null;
			if(person.getFirstname()!=null)
			{
				personList=personDAO.findByFirstname(person.getFirstname(), con);
			}
			else if(person.getLastname()!=null)
			{
				personList=personDAO.findByLastname(person.getLastname(), con);
			}
			else if(person.getCity()!=null)
			{
				personList=personDAO.findByCity(person.getCity(), con);
			}
			else if(person.getState()!=null)
			{
				personList=personDAO.findByState(person.getState(), con);
				
			}
			else if(person.getEmailid()!=null)
			{
				personList=new Person[1];
				personList[0]=personDAO.findByEmailid(person.getEmailid(), con);
			}
			else if(person.getZipcode()!=null)
			{
				personList=personDAO.findByZipcode(person.getZipcode(), con);
			}
			else
			{
				throw new SOAPFaultException(null, "Please select one attribute for search", "Client", 
						 null);
			}
			
			int size=personList.length;
			String result[]=new String[size];
			int index=0;
			for(Person personobj: personList)
			{
				int personid=personobj.getPersonid();
				
				if(personobj.getMembertype().equals("SC"))
				{
					SimplecustomerDAO simpleCustomerDAO=new SimplecustomerDAO();
					SimpleCustomer simplecustomer=simpleCustomerDAO.findByPersonid(personid, con);
					
					result[index]=String.valueOf(personobj.getPersonid()) +":" + personobj.getEmailid() +":" + personobj.getFirstname() +":" + personobj.getLastname() 
							+":" + personobj.getCity() +":" + personobj.getState()+":" + personobj.getMembertype()+ ":" 
							+simplecustomer.getMembershipno() + ":" + String.valueOf(simplecustomer.getOutstandingmovie());
				}
				else
				{
					PremiumcustomerDAO premiumCustomerDAO=new PremiumcustomerDAO();
					PremiumCustomer premiumcustomer=premiumCustomerDAO.findByPersonid(personid, con);
					
					result[index]=String.valueOf(personobj.getPersonid()) +":" + personobj.getEmailid() +":" + personobj.getFirstname() +":" + personobj.getLastname() 
							+":" + personobj.getCity() +":" + personobj.getState()+":" + personobj.getMembertype()+ ":" +premiumcustomer.getMembershipno() + ":" + String.valueOf(premiumcustomer.getOutstandingmovies());
				}
				index++;
				
			}
			
			return result;
		}
		catch(PremiumcustomerException pce) {
			
			throw new SOAPFaultException(null, pce.getMessage(), 
										 "Client", null);
		}
		catch(SimplecustomerException sce) {
			
			throw new SOAPFaultException(null, sce.getMessage(), 
										 "Client", null);
		}
		catch(PersonException pe) {
			
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Search movie by movie attributes
	 * 
	 * @param movie
	 * @return movie list
	 */
	public Movie[] searchMovie(Movie movie) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			MovieDAO movieDAO=new MovieDAO();
			Movie[] movieList=null;
			
			if(movie.getMoviename()!=null)
			{
				movieList=movieDAO.findByMoviename(movie.getMoviename(), con);
			}
			else if(movie.getMoviebanner()!=null)
			{
				movieList=movieDAO.findByMoviebanner(movie.getMoviebanner(), con);
			}
			else if(movie.getCategory()!=null)
			{
				movieList=movieDAO.findByCategory(movie.getCategory(), con);
			}
			else
			{
				throw new SOAPFaultException(null, "Please select one attribute for search", "Client", 
						 null);
			}
			
			return movieList;
			
		}
		catch(MovieException me) {
			
			throw new SOAPFaultException(null, me.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * retrieve simplecustomer by personId
	 * 
	 * @param personId
	 * @return
	 */
	public SimpleCustomer retrieveSimpleCustomerByPersonId(String personId) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			System.out.println("Person Id:" + personId);
			SimplecustomerDAO simpleCustomerDAO=new SimplecustomerDAO();
			
			return simpleCustomerDAO.findByPersonid(Integer.parseInt(personId), con);
			
			
		}catch(SimplecustomerException sce) {
			
			throw new SOAPFaultException(null, sce.getMessage(), 
										 "Client", null);
		}
			catch(SQLException se) {
				
				throw new SOAPFaultException(null, "Please try again later", "Server", 
											 null);
			}
			catch(Exception e) {
				
				throw new SOAPFaultException(null, "Please try again later", "Server", 
											 null);
			}
			finally {
				try {
					con.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	}

	/**
	 * retrieve premium customer by personId
	 * 
	 * @param personId
	 * @return
	 */
	public PremiumCustomer retrievePremiumCustomerByPersonId(String personId) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			PremiumcustomerDAO premiumCustomerDAO=new PremiumcustomerDAO();
			
			return premiumCustomerDAO.findByPersonid(Integer.parseInt(personId), con);
			
			
		}catch(PremiumcustomerException pce) {
			
			throw new SOAPFaultException(null, pce.getMessage(), 
										 "Client", null);
		}
			catch(SQLException se) {
				
				throw new SOAPFaultException(null, "Please try again later", "Server", 
											 null);
			}
			catch(Exception e) {
				
				throw new SOAPFaultException(null, "Please try again later", "Server", 
											 null);
			}
			finally {
				try {
					con.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	}

	/**
	 * Called when person is searched and list of movies issued by that person
	 * 
	 * @param personId person identifier
	 * @return IssuedMovies + Movie Details
	 */
	public String[] retrieveIssuedMoviesByPersonId(String personId) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			IssuedmovieDAO issuedMovieDAO=new IssuedmovieDAO();
			IssuedMovie[] issuedMovieList=issuedMovieDAO.findByPersonid(Integer.parseInt(personId), con);
			
			
			Movie[] movieList=new Movie[issuedMovieList.length];
			MovieDAO movieDAO=new MovieDAO();
			int index=0;
			for(IssuedMovie issuedmovieObj : issuedMovieList)
			{
				movieList[index]=movieDAO.findByMovieId(issuedmovieObj.getMovieid(), con);
				index++;
			}
			String result[] = new String[issuedMovieList.length];
			result=VLMSService.mergeIssuedMovieDetails(issuedMovieList, movieList);
			
			return result;
		}
		catch(MovieException me) {
			
			throw new SOAPFaultException(null, me.getMessage(), 
										 "Client", null);
		}
		catch(IssuedmovieException ime) {
			
			throw new SOAPFaultException(null, ime.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Called when movie is searched
	 * @param movieId movie identifier
	 * @return Movie Details
	 */
	public Movie retrieveMovieById(String movieId) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
		
		MovieDAO movieDAO=new MovieDAO();
		System.out.println(movieId);
		
		return movieDAO.findByMovieId(Integer.parseInt(movieId), con);
		
		}
		catch(MovieException pe) {
					
					throw new SOAPFaultException(null, pe.getMessage(), 
												 "Client", null);
				}
		catch(SQLException se) {
					
					throw new SOAPFaultException(null, "Please try again later", "Server", 
												 null);
				}
		catch(Exception e) {
					
					throw new SOAPFaultException(null, "Please try again later", "Server", 
												 null);
				}
				finally {
					try {
						con.close();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		
	}

	public Person retrievePersonByEmail(String emailid)
	{
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			PersonDAO personDAO=new PersonDAO();
			
			return personDAO.findByEmailid(emailid, con);
			
			
		}
catch(PersonException pe) {
			
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * Called when Movie is searched and list of persons who issued that movie
	 * 
	 * @param movieId movie identifier
	 * @return Person Information + Member Details
	 */
	public String[] retrievePersonsByMovieId(String movieId) {
		Connection con = null;
		String result[];
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			
			IssuedmovieDAO issuedMovieDAO=new IssuedmovieDAO();
			System.out.println(movieId);
			IssuedMovie[] issuedMovieList=issuedMovieDAO.findByMovieid(Integer.parseInt(movieId), con);
			int size=issuedMovieList.length;
			int personIdList[]=new int[size];
			int index=0;
			for(IssuedMovie issuedMovieObj: issuedMovieList)
			{
				personIdList[index]=issuedMovieObj.getPersonid();
				System.out.println("id :" + personIdList[index]);
				index++;
			}
			System.out.println("lenght" + personIdList.length );
			PersonDAO personDAO=new PersonDAO();
			int index1=0;
			result=new String[size];
			result[index1]="";
			int count=0;
			for(int personid : personIdList)
			{
				Person personobj=personDAO.findByPrimaryKey(personid, con);
				
				if(personobj.getMembertype().equals("SC"))
				{
					SimplecustomerDAO simpleCustomerDAO=new SimplecustomerDAO();
					SimpleCustomer simplecustomer=simpleCustomerDAO.findByPersonid(personid, con);
					

					result[index1]=String.valueOf(personobj.getPersonid()) +":" + personobj.getEmailid() +":" + personobj.getFirstname() +":" + personobj.getLastname() 
							+":" + personobj.getCity() +":" + personobj.getState()+":" + personobj.getMembertype()+ ":" 
							+simplecustomer.getMembershipno() + ":" + String.valueOf(simplecustomer.getOutstandingmovie());
				
				
				}
				else
				{
					PremiumcustomerDAO premiumCustomerDAO=new PremiumcustomerDAO();
					PremiumCustomer premiumcustomer=premiumCustomerDAO.findByPersonid(personid, con);
					
					
					result[index1]=String.valueOf(personobj.getPersonid()) +":" + personobj.getEmailid() +":" + personobj.getFirstname() +":" + personobj.getLastname() 
					+":" + personobj.getCity() +":" + personobj.getState()+":" + personobj.getMembertype()+ ":" +premiumcustomer.getMembershipno() + ":" + String.valueOf(premiumcustomer.getOutstandingmovies());
				}
				System.out.println(index1);
				index1++;
				
			}
			for(String r:result)
			{
				System.out.println(r);
			}
			
			con.commit();
			return result;
		}
		catch(PremiumcustomerException pce) {
			
			throw new SOAPFaultException(null, pce.getMessage(), 
										 "Client", null);
		}
		catch(SimplecustomerException sce) {
			
			throw new SOAPFaultException(null, sce.getMessage(), 
										 "Client", null);
		}
		catch(PersonException pe) {
			
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(IssuedmovieException ime) {
			
			throw new SOAPFaultException(null, ime.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Clears the fine charged to the customer for non return of movie till
	 * due date.
	 * @param personId Person identifier
	 * @return Success or error string.
	 */
	public String clearFine(Person oPerson)
	{
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			PersonDAO personDAO=new PersonDAO();
			int personId=oPerson.getPersonid();
			
			if(oPerson.getMembertype().equals("SC"))
			{
				SimplecustomerDAO simpleCustomerDAO=new SimplecustomerDAO();
				SimpleCustomer simpleCustomer=new SimpleCustomer();
				simpleCustomer=simpleCustomerDAO.findByPersonid(personId, con);
				float newBalance=simpleCustomer.getBalance()-oPerson.getFine();
				if(newBalance<0)
				{
					throw new SimplecustomerException("You dont have sufficient funds to clear fine.");
				}
				simpleCustomer.setBalance(newBalance);
				simpleCustomerDAO.update(simpleCustomer.getMembershipno(), simpleCustomer, con);
			}
			
			personDAO.updateFine(personId, oPerson, con);
			con.commit();
			return "Fine Cleared Successfully";
		}

		catch(SimplecustomerException sce) {
			
			throw new SOAPFaultException(null, sce.getMessage(), 
										 "Client", null);
		}
		catch(PersonException pe) {
			
			throw new SOAPFaultException(null, pe.getMessage(), 
										 "Client", null);
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * View the fine charge to the customer.
	 * @param personId Person identifier
	 * @return All the issued movies details and there fine.
	 */
	public String viewFine(String personId)
	{
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");		
			
		}
		catch(SQLException se) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * Creates a new premium member through registration.
	 * @param account Reference to account bean.
	 * @param person Reference to person bean. 
	 * @return Success or error string.
	 */
	public String createPremiumCustomer(Account account, Person person)  throws SOAPFaultException {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			PersonDAO personDAO = new PersonDAO();
			AccountDAO accountDAO = new AccountDAO();
			PremiumcustomerDAO premiumcustomerDAO = new PremiumcustomerDAO();
			
			personDAO.isValid(person);
			accountDAO.isValid(account);
			
			personDAO.isExist(person,con);
			accountDAO.isExist(account,con);
			
			personDAO.insert(person, con);
			Person oPerson = personDAO.findByEmailid(person.getEmailid(), con);
			account.setPersonid(oPerson.getPersonid());
			accountDAO.insert(account, con);
			
				
			PremiumCustomer premiumCustomer = new PremiumCustomer();
			premiumCustomer.setPersonid(oPerson.getPersonid());
			Calendar cal = Calendar.getInstance();
			premiumCustomer.setSubscriptiondate(cal.getTime());
			
			cal.add(Calendar.DATE, 30);		
			premiumCustomer.setDuedate(cal.getTime());				
			premiumCustomer.setAutopay(true);
			premiumCustomer.setSubscriptionfee(30);
			premiumcustomerDAO.insert(premiumCustomer, con);
			con.commit();
			return "Premium Account created successfully";
	
		}catch(AccountException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (PersonException pe) {
			// TODO Auto-generated catch block
			throw new SOAPFaultException(null, pe.getMessage(), "Client", null);
		}
		catch (PremiumcustomerException pce) {
			throw new SOAPFaultException(null, pce.getMessage(), "Server", null);
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}  
catch(Exception e) {
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
// HARSHIL SHAH //
	
// mANDAR MUNSHi //
	/**
	 * Creates a new simple customer through registration.
	 * @param account Reference to account bean.
	 * @param person Reference to person bean. 
	 * @return Success or error string.
	 */
	public String createSimpleCustomer(Account account, Person person) throws SOAPFaultException {
		
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			PersonDAO personDAO = new PersonDAO();
			AccountDAO accountDAO = new AccountDAO();
			
			SimplecustomerDAO simplecustomerDAO = new SimplecustomerDAO();
			
			personDAO.isValid(person);
			accountDAO.isValid(account);
			
			personDAO.isExist(person,con);
			
			accountDAO.isExist(account,con);
			
			personDAO.insert(person, con);
			Person oPerson = personDAO.findByEmailid(person.getEmailid(), con);
			account.setPersonid(oPerson.getPersonid());
			accountDAO.insert(account, con);
			
			
			SimpleCustomer simplecustomer = new SimpleCustomer();
			simplecustomer.setPersonid(oPerson.getPersonid());
			simplecustomerDAO.insert(simplecustomer, con);
			con.commit();
			return "Account created successfully";
	
		}catch(AccountException ae) {
			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (PersonException pe) {
			// TODO Auto-generated catch block
			throw new SOAPFaultException(null, pe.getMessage(), "Client", null);
		}
		catch (SimplecustomerException sce) {
			throw new SOAPFaultException(null, sce.getMessage(), "Server", null);
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}  
catch(Exception e) {
			
			throw new SOAPFaultException(null, "Please try again later", "Server", 
										 null);
		}
		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
	}

	/**
	 * Authenticate user credentials.
	 * @param account Reference to account bean.
	 * @return Success or error string.
	 */
	public Account logIn(Account account) throws SOAPFaultException {
		
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
		
			AccountDAO accountDAO = new AccountDAO();
			
			return accountDAO.findAccount(account, con);		
			
			
			
		}catch(AccountException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	/**
	 *  Retrieve currently issued movies by person id.
	 *  
	 *  @param personId Input personId. 
	 *  @return Issued Movies information. 
	 */
	public IssuedMovie[] retrieveIssuedMovieByPersonId(String personId) {
		
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
		
			int iPersonId = Integer.parseInt(personId);
			
			IssuedmovieDAO issuedmovieDAO = new IssuedmovieDAO();
			
			return issuedmovieDAO.findCurrentByPersonid(iPersonId, con);		
			
			
			
		}catch(IssuedmovieException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}
	
	/**
	 * Retrieve all movies issued by person till now.
	 * @param personId Input personId.
	 * @return Issued Movies information.
	 */
	public IssuedMovie[] retrieveAllMoviesByPersonId(String personId) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
		
			int iPersonId = Integer.parseInt(personId);
			
			IssuedmovieDAO issuedmovieDAO = new IssuedmovieDAO();
		 
			return issuedmovieDAO.findByPersonid(iPersonId, con);		
			
			
			
		}catch(IssuedmovieException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Retrieve person by its person identifier.
	 * @param personId Input personId
	 * @return Person information.
	 */
	public Person retrievePersonById(String personId) {
		
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
		
			int iPersonId = Integer.parseInt(personId);
			
			PersonDAO personDAO = new PersonDAO();
			
			return personDAO.findByPrimaryKey(iPersonId, con);		
			
			
			
		}catch(PersonException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	/**
	 * Updates member information according to the input specified.
	 * 
	 * @param account
	 *            Reference to account bean.
	 * @param person
	 *            Reference to person bean.
	 * @return Success or error string.
	 */
	public String updateMember(Account account, Person person) {

		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			PersonDAO personDAO = new PersonDAO();
			AccountDAO accountDAO = new AccountDAO();

			personDAO.isValid(person);
			String userName = account.getUsername();
			if (userName == null || userName.length() < 8
					|| userName.length() > 45) {
				throw new AccountException(
						"Invalid Username Format. User Name should be of more than 8 characters and less than 45 character");
			}

			// accountDAO.isValid(account);

			personDAO.update(person.getPersonid(), person, con);
			accountDAO.update(person.getPersonid(), account, con);

			con.commit();

			return "Account updated successfully";

		} catch (PersonException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);
		} catch (AccountException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later",
					"Server", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later",
					"Server", null);
		}

		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Updates member information according to the input specified.
	 * 
	 * @param account
	 *            Reference to account bean.
	 * @param person
	 *            Reference to person bean.
	 * @return Success or error string.
	 */
	public String updatePassword(Account account) {

		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			AccountDAO accountDAO = new AccountDAO();
			accountDAO.isValid(account);
			accountDAO.update(account.getPersonid(), account, con);
			con.commit();
			return "Password updated successfully";
		} catch (AccountException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later",
					"Server", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later",
					"Server", null);
		}

		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Search movie based on movie parameters.
	 * @param movie Reference to Movie bean.
	 * @return Movies information.
	 */
	public Movie[] retrieveMovies(Movie movie) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			MovieDAO movieDAO = new MovieDAO();
			
			if(movie.getMoviename() != null && !movie.getMoviename().equals("")) {
				return movieDAO.findByMoviename(movie.getMoviename(), con);
			}
			else if(movie.getMoviebanner() != null && !movie.getMoviebanner().equals("")) {
				return movieDAO.findByMoviebanner(movie.getMoviebanner(), con);				
			}
			else if(movie.getCategory() != null && !movie.getCategory().equals("")) {
				return movieDAO.findByCategory(movie.getCategory(), con);				
			}
			else {
				throw new MovieException("Invalid Search Parameters");
			}		
			
		}catch(MovieException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Check out movie from the cart of the person. 
	 * @param personId Person identifier.
	 * @param PremiumCustomer 
	 * @return Success or error string.
	 */
	public MovieIssue[] checkoutMovie(String personId) {
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			SimplecustomerDAO scDAO = new SimplecustomerDAO();
			PremiumcustomerDAO pcDAO = new PremiumcustomerDAO();
			MovieDAO movieDAO = new MovieDAO();
			IssuedmovieDAO issuedMovieDAO = new IssuedmovieDAO();
			
			int outstandingMovies;
			float totalAmount = 0;
			SimpleCustomer sc = null;
			PremiumCustomer pc = null;
			Movie[] aMovie;
			IssuedMovie aIssuedMovie[];
			
			int iPersonId = Integer.parseInt(personId);
			
			CartDAO oCartDAO = new CartDAO();
			Cart[] aCart = oCartDAO.findByPersonId(iPersonId, con);
			
			aMovie = new Movie[aCart.length];
			
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.findByPrimaryKey(iPersonId, con);
			
			if(person.getMembertype().equals("SC")) {
				sc = scDAO.findByPersonid(iPersonId, con);
				outstandingMovies = sc.getOutstandingmovie();
				System.out.println("Outstanding movies" + outstandingMovies + "  Cart length: " + aCart.length);
				if((outstandingMovies - aCart.length) < 0){
					throw new CartException("You can have only 2 movies outstanding at a time");
				}			
				
			}
			else {
				pc = pcDAO.findByPersonid(iPersonId, con);
				outstandingMovies = pc.getOutstandingmovies();
				if((outstandingMovies - aCart.length) < 0){
					throw new CartException("You can have only 10 movies outstanding at a time");
				}
			}
			
						
			for(int i=0;i<aCart.length;i++) {
				
				aMovie[i] = movieDAO.findByMovieId(aCart[i].getMovieid(), con);
				totalAmount += aMovie[i].getRentamount();
				if(aMovie[i].getCopiesavailable() < 0) {
					throw new MovieException(aMovie[i].getMoviename() + " Movie not available");
				}
			}
			
			if(person.getMembertype().equals("SC")) {
				if(totalAmount > sc.getBalance()) {
					throw new SimplecustomerException("Insufficient Balance");
				}
			}
			
			
			aIssuedMovie = new IssuedMovie[aCart.length];
			
			for(int i=0;i<aCart.length;i++) {
				
				IssuedMovie issuedmovie = new IssuedMovie();
				
				issuedmovie.setMovieid(aCart[0].getMovieid());
				issuedmovie.setPersonid(iPersonId);
				Calendar cal = Calendar.getInstance();
				issuedmovie.setIssuedate(cal.getTime());
				issuedmovie.setIssuedstatus("CURR");
								
				cal.add(Calendar.DATE, 7);				
				issuedmovie.setDuedate(cal.getTime());				
				
				issuedmovie = issuedMovieDAO.insert(issuedmovie, con);
				Movie oMovie = new Movie(movieDAO.findByMovieId(aCart[i].getMovieid(), con));
				System.out.println("Movie Copies before: " + oMovie.getCopiesavailable());
				oMovie.setCopiesavailable(oMovie.getCopiesavailable()-1);
				System.out.println("Movie Copies after : " + oMovie.getCopiesavailable());
				movieDAO.update(aCart[i].getMovieid(), oMovie, con);
				
				oCartDAO.delete(aCart[i].getCartid(), con);
				
				aIssuedMovie[i] = issuedmovie;
				aMovie[i] = oMovie;
			}	
			
			if(person.getMembertype().equals("SC")) {
					SimpleCustomer simplecustomer = new SimpleCustomer(sc);
					simplecustomer.setOutstandingmovie(simplecustomer.getOutstandingmovie() - aCart.length);
					simplecustomer.setBalance(simplecustomer.getBalance()-totalAmount);
					scDAO.update(sc.getMembershipno(), simplecustomer , con);					
			}
			else {
				PremiumCustomer premiumcustomer = new PremiumCustomer(pc);
				premiumcustomer.setOutstandingmovies(premiumcustomer.getOutstandingmovies() - aCart.length);				
				pcDAO.update(pc.getMembershipno(), premiumcustomer , con);
			}	
			
			MovieIssue[] aMovieIssue = MovieIssue.mixObjects(aMovie, aIssuedMovie);
			con.commit();
			return aMovieIssue;	
			
		}catch(NumberFormatException nfe) {
			throw new SOAPFaultException(null, "Invalid personId format", "Client", null);
		}
		catch(SimplecustomerException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(PremiumcustomerException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(MovieException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(IssuedmovieException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(PersonException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(CartException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Retrieves generated bill information for the person.
	 * @param personId Person identifier 
	 * @return Generated bill in form of table.
	 */
	public String[] generateBill(String personId) {

		return null;
	}

	/**
	 * view cart. 
	 * @param MovieId Movie identifier
	 * @param PersonId Person identifier
	 * @return Success or Error string.
	 */
	public CartMovie[] viewCart(String sPersonId) throws SOAPException {

		Connection con = null;
		
		try {
			
			con = DriverManager.getConnection("jdbc:user_mysql:");
			int iPersonId = Integer.parseInt(sPersonId);
			
			CartDAO cartDao = new CartDAO();
			MovieDAO movieDao = new MovieDAO();
			System.out.println("Entered in Function");
			Cart[] aCart = cartDao.viewCart(iPersonId, con);
			System.out.println("Entered in Function 1");
			Movie[] aMovie = new Movie[aCart.length];
			System.out.println("Entered in Function 2");
		
			for(int i=0;i<aCart.length;i++) {
				aMovie[i] = movieDao.findByMovieId(aCart[i].getMovieid(), con);				
			}
			
			System.out.println("Entered in Function 3");
			
			return CartMovie.mixObjects(aMovie, aCart);			
			
						
			
		}catch(NumberFormatException ae) {
			throw new SOAPFaultException(null, "Invalid Parameters", "Client", null);			
		}
		catch(MovieException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}		
		catch(CartException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	/**
	 * Add a movie in the cart. 
	 * @param MovieId Movie identifier
	 * @param PersonId Person identifier
	 * @return Success or Error string.
	 */
	public CartMovie[] addMovieInCart(String sMovieId, String sPersonId) throws SOAPException {

		Connection con = null;
		
		try {
			
			int iMovieId = Integer.parseInt(sMovieId);
			int iPersonId = Integer.parseInt(sPersonId);
			
			con = DriverManager.getConnection("jdbc:user_mysql:");
					
			MovieDAO movieDao = new MovieDAO();
			
			//Checks whether movie id exists in system.if not then throws an error
			movieDao.findByMovieId(iMovieId, con);
			
			/*if (oMovie.getCopiesavailable() <=0) {
				throw new MovieException("Movie out of stock");
			}*/
			CartDAO cartDao = new CartDAO();
			cartDao.insert(iMovieId,iPersonId, con);
			con.commit();
			
			Cart[] aCart = cartDao.viewCart(iPersonId, con);
			System.out.println("Done: " + aCart.length);
			
			Movie[] aMovie = new Movie[aCart.length];
		
			for(int i=0;i<aCart.length;i++) {
				aMovie[i] = movieDao.findByMovieId(aCart[i].getMovieid(), con);				
			}
			
			return CartMovie.mixObjects(aMovie, aCart);
			
			
						
			
		}catch(NumberFormatException ae) {
			throw new SOAPFaultException(null, "Invalid Parameters", "Client", null);			
		}
		catch(MovieException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(CartException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

	/**
	 * Delete movie from cart.
	 * @param cartId Cart identifier.
	 * @return Success or error string.
	 */
	public String deleteMovieFromCart(String sCartId) throws SOAPFaultException {
		
		Connection con = null;
		
		try {
			
			int iCartId = Integer.parseInt(sCartId);			
			
			con = DriverManager.getConnection("jdbc:user_mysql:");
		
			CartDAO cartDao = new CartDAO();
			cartDao.delete(iCartId, con);
			con.commit();
			return "Movie Deleted Successfully from Cart";
						
			
		}catch(NumberFormatException ae) {
			throw new SOAPFaultException(null, "Invalid Parameters", "Client", null);			
		}		
		catch(CartException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

	/**
	 * Add balance to the account of customer.
	 * 
	 * @param personId Person identifier.
	 * @param amount Amount to be added in balance.
	 * @return Success or error string.
	 */
	public String addBalanceToAccount(String personId, String amount) {
		
		Connection con = null;
		
		try {
			
			int iPersonId = Integer.parseInt(personId);	
			float fAmount = Float.parseFloat(amount);
			
			System.out.println("Famount: " + fAmount);
			if(fAmount < 0) {
				throw new SimplecustomerException("Invalid Amount Value. Please Enter a Positive Value");
			}
			
			con = DriverManager.getConnection("jdbc:user_mysql:");
		
			SimplecustomerDAO scDAO = new SimplecustomerDAO();
			
			SimpleCustomer oSC = new SimpleCustomer(scDAO.findByPersonid(iPersonId, con));
			oSC.setBalance(oSC.getBalance() + fAmount);
			
			System.out.println("Balance: " + oSC.getBalance());
			scDAO.update(oSC.getMembershipno(), oSC, con);
			//return cartDao.insert(iMovieId,iPersonId, con);
			con.commit();
			return "Balance Added Successfully";
						
			
		}catch(NumberFormatException ae) {
			throw new SOAPFaultException(null, "Invalid Parameters", "Client", null);			
		}		
		catch(SimplecustomerException ae) {
			throw new SOAPFaultException(null, ae.getMessage(), "Server", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	

	/**
	 * Add balance to the account of customer.
	 * 
	 * @param personId Person identifier.
	 * @param amount Amount to be added in balance.
	 * @return Success or error string.
	 */
	public String validateCart(String personId) {
		
		Connection con = null;
		
		try {
			
			con = DriverManager.getConnection("jdbc:user_mysql:");
			SimplecustomerDAO scDAO = new SimplecustomerDAO();
			PremiumcustomerDAO pcDAO = new PremiumcustomerDAO();
			MovieDAO movieDAO = new MovieDAO();
			
			
			int outstandingMovies;
			float totalAmount = 0;
			SimpleCustomer sc = null;
			PremiumCustomer pc = null;
			Movie[] aMovie;
			
			int iPersonId = Integer.parseInt(personId);
			
			CartDAO oCartDAO = new CartDAO();
			Cart[] aCart = oCartDAO.findByPersonId(iPersonId, con);
			
			aMovie = new Movie[aCart.length];
			
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.findByPrimaryKey(iPersonId, con);
			
			if(person.getMembertype().equals("SC")) {
				sc = scDAO.findByPersonid(iPersonId, con);
				outstandingMovies = sc.getOutstandingmovie();
				System.out.println("Outstanding movies" + outstandingMovies + "  Cart length: " + aCart.length);
				if((outstandingMovies - aCart.length) < 0){
					throw new CartException("You Already Issued 2 movies");
				}			
				
			}
			else {
				pc = pcDAO.findByPersonid(iPersonId, con);
				outstandingMovies = pc.getOutstandingmovies();
				if((outstandingMovies - aCart.length) < 0){
					throw new CartException("You can have only 10 movies outstanding at a time");
				}
			}
			
						
			for(int i=0;i<aCart.length;i++) {
				
				aMovie[i] = movieDAO.findByMovieId(aCart[i].getMovieid(), con);
				totalAmount += aMovie[i].getRentamount();
				System.out.println("copies :" + aMovie[i].getCopiesavailable());
				if(aMovie[i].getCopiesavailable() == 0) {
					throw new MovieException(aMovie[i].getMoviename() + " Movie not available");
				}
			}
			
			if(person.getMembertype().equals("SC")) {
				if(totalAmount > sc.getBalance()) {
					throw new SimplecustomerException("Insufficient Balance");
				}
			}
			
			return "true";
			
		}catch(NumberFormatException nfe) {
			throw new SOAPFaultException(null, "Invalid personId format", "Client", null);
		}
		catch(SimplecustomerException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(PremiumcustomerException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(MovieException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}		
		catch(PersonException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch(CartException ae) {			
			throw new SOAPFaultException(null, ae.getMessage(), "Client", null);			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SOAPFaultException(null, "Please try again later", "Server", null);
		}
		
		finally {
			try {
				con.rollback();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
	
	}
	
	public int getMaxPersonId()
	{
		int personId = 0;
		try
		{
			Connection con = DriverManager.getConnection("jdbc:user_mysql:");
			Person person[] = new PersonDAO().findAll(con);

			for (int index = 0; index < person.length; index++)
			{
				if (personId < person[index].getPersonid())
				{
					personId = person[index].getPersonid();
				}
			}
		}
		catch (Exception e)
		{
			// return zero to signify error.
		}
		return personId;
	}
	
	public int getMaxMovieId()
	{
		int movieId = 0;
		try
		{
			Connection con = DriverManager.getConnection("jdbc:user_mysql:");
			Movie movie[] = new MovieDAO().findAll(con);

			for (int index = 0; index < movie.length; index++)
			{
				if (movieId < movie[index].getMovieid())
				{
					movieId = movie[index].getMovieid();
				}
			}
		}
		catch (Exception e)
		{
			// return zero to signify error.
		}
		return movieId;
	}	
// mANDAR MUNSHi //	
}
